<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div id="app">
        {{$store.state.count}}

        平方为: {{$store.getters.lifangCount}}
        count * 10 为: {{$store.getters.multiCount(10)}}

        <button @click="addOne">+1</button>

        <comp></comp>
    </div>
    <script src="./node_modules/vue/dist/vue.js"></script>
    <script src="./node_modules/vuex/dist/vuex.js"></script>
    <script>
        // 创建出来一个新的vuex仓库
        const store = new Vuex.Store({
            state: {
                count: 0
            },
            mutations: {
                increment (state, value) {
                    state.count += value;
                }
            },
            // vuex中的计算属性
            getters:{
                lifangCount: function(state, getters){
                    return getters.sqrtCount * state.count;
                },
                sqrtCount: function(state){
                    return state.count * state.count;
                },
                // multiCount: function(state){
                //     return function(num){
                //         return num * state.count;
                //     }
                // },
                multiCount: state => num => num * state.count
            }
        })


        Vue.component("comp", {
            template: "<div>{{$store.state.count}}</div>"
        })


        var vm = new Vue({
            el: "#app",
            data: {
            },
            store,
            methods: {
                addOne(){
                    this.$store.commit("increment", 10)
                }
            }
        })
    </script>
</body>

</html>